import 'package:badges/badges.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_getx/app/base/load_image.dart';
import 'package:flutter_getx/core/getx/control/cart_control.dart';
import 'package:flutter_getx/ext/ext.dart';
import 'package:flutter_getx/res/colors/colors.dart';
import 'package:flutter_getx/res/colors/colors_dark.dart';
import 'package:flutter_getx/res/colors/theme_colors.dart';
import 'package:flutter_getx/res/dimens/dimens.dart';
import 'package:flutter_getx/res/strings/strings.dart';
import 'package:flutter_getx/res/styles.dart';
import 'package:get/get.dart';

import 'my_widgets.dart';

class HomeAppBar extends StatefulWidget {
  const HomeAppBar(this.scrollController,
      {Key? key, this.menuFunc, this.cartFunc, this.searchFunc, this.searchTip = ''})
      : super(key: key);

  final ScrollController scrollController;
  final Function()? menuFunc;
  final Function()? cartFunc;
  final Function()? searchFunc;
  final String searchTip;


  @override
  _HomeAppBarState createState() => _HomeAppBarState();
}

class _HomeAppBarState extends State<HomeAppBar> {
  double _searchOpacity = 0;

  @override
  void initState() {
    super.initState();

    widget.scrollController
        .addListener(() {
      var offset = widget.scrollController.offset;
      if (offset <= 40) {
        setState(() {
          _searchOpacity = offset < 20 ? 0 : (offset - 20) / 20;
        });
        //print("_searchOpacity = $_searchOpacity");
      } else {
        if (_searchOpacity != 1) {
          setState(() {
            _searchOpacity = 1;
          });
        }
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return SliverAppBar(
      //brightness: Brightness.light,
      systemOverlayStyle: SystemUiOverlayStyle.dark, //状态栏文字黑色 不再使用 brightness属性
      primary: true,
      backgroundColor: Get.isDarkMode ? gradientStartDark : gradientStart,
      elevation: Dp.dp_5,
      title: Text(
        'appName'.tr,
        style: appbarBlack,
      ),
      titleSpacing: 0,
      leading: IconButton(
        icon: LoadImage('ic_menu'.png, width: 18, height: 16, fit: BoxFit.fill,) /*Icon(
          Icons.menu,
          color: Colors.black,
        )*/,
        tooltip: 'Navigation',
        onPressed: widget.menuFunc,
      ),
      pinned: true,
      expandedHeight: Dp.dp_110,
      flexibleSpace: FlexibleSpaceBar(
        background: Container(
          alignment: Alignment.bottomCenter,
          decoration: BoxDecoration(
            gradient: gradientTheme(),
          ),
          child: Padding(
            padding:
            EdgeInsets.symmetric(horizontal: Dp.dp_10, vertical: Dp.dp_12),
            child: Visibility(
              visible: _searchOpacity <= 0.9,
              child: InkWell(
                onTap: widget.searchFunc,
                child: Material(
                  borderRadius: BorderRadius.circular(Dp.dp_4),
                  color: backgroundColor,
                  elevation: Dp.dp_5,
                  child: Container(
                    width: double.infinity,
                    height: Dp.dp_40,
                    alignment: Alignment.centerLeft,
                    child: Row(
                      children: [
                        SizedBox(
                          width: Dp.dp_20,
                        ),
                        LoadImage(
                          'ic_search_mini'.png,
                          width: Dp.dp_15,
                          height: Dp.dp_15,
                          fit: BoxFit.fill,
                          color: color_999,
                        ),
                        SizedBox(
                          width: Dp.dp_10,
                        ),
                        Text(
                          widget.searchTip,
                          style: TextStyle(color: color_999, fontSize: Sp.sp14),
                        )
                      ],
                    ),
                  ),
                ),
              ),
            ),
          ),
        ),
      ),
      actions: [
        Visibility(
          visible: _searchOpacity != 0,
          child: Opacity(
            opacity: _searchOpacity,
            child: _searchWidget(
                _searchOpacity == 0 ? null : widget.searchFunc),
          ),
        ),
        cartWidget(widget.cartFunc),
        _rightSpace,
      ],
    );
  }
}

const Widget _rightSpace = SizedBox(width: 12,);

Widget _searchWidget(Function()? searchFunc) {
  return Visibility(
    visible: searchFunc != null,
    child: IconButton(
      icon: LoadImage(
        'ic_search'.png,
        width: Dp.dp_26,
        height: Dp.dp_26,
        fit: BoxFit.fill,
      ),
      tooltip: 'Search',
      onPressed: searchFunc,
    ),
  );
}

Widget cartWidget(Function()? cartFunc) {
  return Visibility(
    visible: cartFunc != null,
    child: Obx(() {
      return Badge(
        toAnimate: false,
        ignorePointer: true,
        shape: BadgeShape.square,
        badgeColor: color_FF751D,
        padding: EdgeInsets.symmetric(vertical: Dp.dp_1, horizontal: Dp.dp_5),
        borderRadius: BorderRadius.circular(Dp.dp_15),
        badgeContent: Text(
            '${CartControl.to.cartNum.value}',
            style: TextStyle(color: Colors.white, fontSize: Sp.sp13)),
        showBadge: CartControl.to.cartNum.value > 0,
        position: BadgePosition.topEnd(top: 5, end: -1),
        child: IconButton(
          icon: LoadImage(
            'ic_cart'.png,
            width: Dp.dp_26,
            height: Dp.dp_26,
            fit: BoxFit.fill,
          ),
          tooltip: 'Cart',
          onPressed: cartFunc,
        ),
      );
    }),
  );
}